package com.wy.sx_restclient.controller;

import com.wy.sx_restclient.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/9/916:24
 */

@RestController
@RequestMapping("client")
public class ClientController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("list")
    public List<User> list(String name){
        User user =new User();
        user.setId("1111");
        user.setName("2222");
       // ResponseEntity<List> forEntity = restTemplate.getForEntity("http://localhost:8080/user/list1", List.class, map);
       // ResponseEntity<List> forEntity = restTemplate.postForEntity("http://localhost:8080/user/list2",user,List.class);
       // return forEntity.getBody();
        //只返回结果
       // List<User> userList = restTemplate.getForObject("http://localhost:8080/user/list1", List.class, map);
        //List<User> userList = restTemplate.postForObject("http://localhost:8080/user/list1", List.class, map);
        //exchange方法，解决put和delete没有返回值的问题
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 发送请求
        HttpEntity<User> entity = new HttpEntity<User>(user, headers);
        ResponseEntity<List> exchange = restTemplate.exchange("http://localhost:8080/user/list1", HttpMethod.PUT, entity, List.class, user);
        return exchange.getBody();
    }
}
